# Maintainer: omni <omni+alpine@hack.org>
maintainer="omni <omni+alpine@hack.org>"
pkgname=minijinja-cli
pkgver=2.4.0
pkgrel=1
pkgdesc="CLI using MiniJinja to render Jinja2 templates from the command line to stdout"
_pkgname=minijinja
url="https://docs.rs/minijinja/latest/minijinja/"
arch="all"
license="Apache-2.0"
makedepends="cargo
	cargo-auditable
	py3-gpep517
	py3-maturin
	py3-setuptools
	sequoia-sq
	"
checkdepends="py3-pytest"
subpackages="
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	py3-$_pkgname:py3
	"
source="$_pkgname-$pkgver.tar.gz::https://github.com/mitsuhiko/minijinja/archive/refs/tags/$pkgver.tar.gz
	disable-test_random-patch
	"
builddir="$srcdir"/"$_pkgname-$pkgver"

prepare() {
	default_prepare

	case "$CARCH" in
	arm*|x86)
		patch -Np1 < "$srcdir"/disable-test_random-patch ;;
	esac

	cargo fetch --target="$CTARGET" --locked
}

build() {
	ASSET_OUT_DIR=asset_out_dir cargo auditable build --release --frozen \
		--all-features --package "$pkgname"

	cd minijinja-py
	gpep517 build-wheel \
		--wheel-dir .dist\
		--config-json '{"build-args": "--locked", "build-option": "--release"}' \
		--output-fd 3 3>&1 >&2
}

check() {
	cargo test --all --frozen

	cd minijinja-py
	python3 -m venv --clear --without-pip --system-site-packages .testenv
	.testenv/bin/python3 -m installer .dist/*.whl
	.testenv/bin/python3 -m pytest
}

package() {
	install -Dm755 target/release/"$pkgname" \
		-t "$pkgdir"/usr/bin/

	_asset_out_dir="$pkgname/asset_out_dir"
	install -Dm644 "$_asset_out_dir"/man/* \
		-t "$pkgdir"/usr/share/man/man1/
	install -Dm644 "$_asset_out_dir"/completions/"$pkgname".bash \
		"$pkgdir"/usr/share/bash-completion/completions/"$pkgname"
	install -Dm644 "$_asset_out_dir"/completions/"$pkgname".fish \
		-t "$pkgdir"/usr/share/fish/vendor_completions.d/
	install -Dm644 "$_asset_out_dir"/completions/_"$pkgname" \
		-t "$pkgdir"/usr/share/zsh/site-functions/

	cd minijinja-py
	python3 -m installer -d "$pkgdir" .dist/*.whl
}

py3() {
	pkgdesc="Python bindings for MiniJinja, a template engine compatible with Jinja/Jinja2"

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/python3* "$subpkgdir"/usr/lib/
}

sha512sums="
662978666a5a44f089bc3dcfbe6eb25b2738a497a3e00ede63237204437ce36a003b78af764e0510e3fc8f7a809b90516750de1cfde6b548fe21919e6b659c2c  minijinja-2.4.0.tar.gz
87cb2581d3c4c3461528c71718ff9d5d8418048c477720cfeba46f1915ef1bfa64a91eb9a6d2c6eb4cdba49f295d12605c8369090f931599ed231280490a4462  disable-test_random-patch
"
